package com.youedata.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.youedata.modular.entity.GoodsInfo;
import com.youedata.modular.entity.GoodsInfoImgRel;
import com.youedata.modular.entity.SyTravelInfo;
import com.youedata.modular.entity.TravelInfoImgRel;
import com.youedata.modular.mapper.GoodsInfoMapper;
import com.youedata.modular.mapper.SyTravelInfoMapper;
import com.youedata.modular.model.dto.GoodsInfoDTO;
import com.youedata.modular.model.dto.SyTravelInfoDTO;
import com.youedata.modular.service.IGoodsInfoImgRelService;
import  com.youedata.modular.service.ISyTravelInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youedata.modular.service.ITravelInfoImgRelService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 建湖文广旅 服务实现类
 * </p>
 *
 * @author hst
 * @date 2021-08-06
 */
@Service
@Transactional(rollbackFor = Exception.class)

public class
SyTravelInfoServiceImpl extends ServiceImpl<SyTravelInfoMapper, SyTravelInfo> implements ISyTravelInfoService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private SyTravelInfoMapper syTravelInfoMapper;

    @Autowired
    private ITravelInfoImgRelService travelInfoImgRelService;


    @Override
    public List queryFuzzySearch(Page page, SyTravelInfo syTravelInfo) {
        return syTravelInfoMapper.queryFuzzySearch(page, syTravelInfo);
    }


    @Override
    public void saveSelective(SyTravelInfoDTO syTravelInfoDTO) {
        SyTravelInfo syTravelInfo = new SyTravelInfo();
        BeanUtils.copyProperties(syTravelInfoDTO, syTravelInfo);
        syTravelInfo.setCreateUser(Long.valueOf(UserHolder.getUserId()));
        syTravelInfo.setCreateTime(LocalDate.from(LocalDateTime.now()));
        syTravelInfoMapper.insert(syTravelInfo);

        String topImgUrl = syTravelInfoDTO.getTopImgUrl();
        if (ToolUtil.isNotEmpty(topImgUrl)) {
            List<TravelInfoImgRel> travelInfoImgRels = Lists.newArrayList();
            String[] topImgUrls = topImgUrl.split(",");

            for (String imgUrl : topImgUrls) {
                TravelInfoImgRel travelInfoImgRel = new TravelInfoImgRel();
                travelInfoImgRel.setTravelId(syTravelInfo.getId());
                travelInfoImgRel.setTopImgUrl(imgUrl);
                travelInfoImgRels.add(travelInfoImgRel);
            }

            travelInfoImgRelService.saveBatch(travelInfoImgRels);
        }


    }

    @Override
    public SyTravelInfo detailById(String id,String countFlag) {
        SyTravelInfo syTravelInfo = this.getById(id);

        if (StringUtils.equalsIgnoreCase("Y", countFlag)) {
            syTravelInfo.setPageView(syTravelInfo.getPageView() + 1);
            this.updateById(syTravelInfo);
        }


        List<TravelInfoImgRel> travelInfoImgRels = this.travelInfoImgRelService.list(new LambdaQueryWrapper<TravelInfoImgRel>().eq(TravelInfoImgRel::getTravelId, id));
        if (ToolUtil.isNotEmpty(travelInfoImgRels)) {
//            StringBuffer urls = new StringBuffer();
            StringBuffer urlsFull = new StringBuffer();
            List<String> topImgUrls = Lists.newArrayList();
            travelInfoImgRels.forEach(x -> {
                if (ToolUtil.isNotEmpty(x.getTopImgUrl())) {
//                    urls.append(x.getTopImgUrl()).append(",");
//                    x.setTopImgUrl(webServerUrl + x.getImageTitle());
                    urlsFull.append(x.getTopImgUrl()).append(",");
                    topImgUrls.add(x.getTopImgUrl());
                }
            });
//            travelInfo.setUrl(urls.toString());
//            travelInfo.setUrlFull(urlsFull.toString());
            syTravelInfo.setTopImgUrl(urlsFull.toString());
            syTravelInfo.setTopImgUrls(topImgUrls);
        }
        return syTravelInfo;
    }

    @Override
    public void updateSyTravelInfo(SyTravelInfoDTO syTravelInfoDTO) {
        List<TravelInfoImgRel> travelInfoImgRelList = Lists.newArrayList();

        SyTravelInfo syTravelInfo = new SyTravelInfo();
        BeanUtils.copyProperties(syTravelInfoDTO, syTravelInfo);
        syTravelInfo.setUpdateUser(Long.valueOf(UserHolder.getUserId()));
        syTravelInfo.setUpdateTime(LocalDateTime.now());
        syTravelInfoMapper.updateById(syTravelInfo);

        String topImgUrl = syTravelInfoDTO.getTopImgUrl();
        if (StringUtils.contains(topImgUrl, ",")) {
            String[] urls = topImgUrl.split(",");
            for (int i = 0; i < urls.length; i++) {
                TravelInfoImgRel travelInfoImgRel = new TravelInfoImgRel();
                travelInfoImgRel.setTravelId(syTravelInfo.getId());
                travelInfoImgRel.setTopImgUrl(urls[i]);

                travelInfoImgRelList.add(travelInfoImgRel);
            }
            this.travelInfoImgRelService.remove(new LambdaQueryWrapper<TravelInfoImgRel>().eq(TravelInfoImgRel::getTravelId, syTravelInfo.getId()));
            this.travelInfoImgRelService.saveBatch(travelInfoImgRelList);
        }
    }
}

